﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using Windows.Storage;
using Windows.UI.Xaml.Navigation;
using Windows.Media;
using Windows.ApplicationModel.DataTransfer;
using System.Diagnostics;
using Windows.UI.Core;
using Windows.UI;
using Windows.UI.Notifications;
using Windows.Data.Xml.Dom;




// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace WRS
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class Spelling : Page
    {

        private Random rng = new Random();
        private TransformGroup _transformGroup;
        private TranslateTransform _translate;
        private ScaleTransform _scale;
        private RotateTransform _rotate;
        private bool forceManipulationsToEnd;
        private string directory = "ms-appx:/images/";
        private string picDirectory = "ms-appx:/pictures/";
        string[] Alphabets = new string[] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };
        private string fileType = ".png";
        private Queue<string> question = new Queue<string>();
        private Queue<string> Picture = new Queue<string>();
        private Queue<string> question1 = new Queue<string>();
        private Queue<string> Picture1 = new Queue<string>();
        private string word;
        private int score;
        private string pic;
        private string title1;
        string[] randLetter = new string[6];
        
        
        
        
        public Spelling()
        {
            this.InitializeComponent();
            score = 0;
            question.Enqueue("cat");
            question.Enqueue("dog");
            question.Enqueue("rat");
            question.Enqueue("cat");
            question1.Enqueue("bat");
            question1.Enqueue("cap");
            question1.Enqueue("net");
            question1.Enqueue("cat");

            Picture.Enqueue("cat");
            Picture.Enqueue("dog");
            Picture.Enqueue("rat");
            Picture.Enqueue("cat");
            Picture1.Enqueue("bat");
            Picture1.Enqueue("cap");
            Picture1.Enqueue("net");
            Picture1.Enqueue("cat");

            //question.Enqueue("vat");
            //question.Enqueue("bad");
            //question.Enqueue("fad");
            //question.Enqueue("sag");
            //question.Enqueue("sat");
            //question.Enqueue("rat");
            //question.Enqueue("fan");
            //question.Enqueue("van");
            //question.Enqueue("hat");
            //question.Enqueue("sap");
            //question.Enqueue("tap");
            //question.Enqueue("had");
            //question.Enqueue("mad");
            //question.Enqueue("ran");
            //question.Enqueue("cap");
            //question.Enqueue("man");
            //question.Enqueue("cab");
            //question.Enqueue("nab");
            //question.Enqueue("pad");
            //question.Enqueue("bag");

        }

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            var title = e.Parameter;
            title1 = title.ToString();
        }

        private void btn1_Click(object sender, RoutedEventArgs e)
        {
            this.Frame.Navigate(typeof(BlankPage));
        }

        private void img1_ManipulationStarting(object sender, ManipulationStartingRoutedEventArgs e)
        {
            rectangle(img1);
            forceManipulationsToEnd = false;
            e.Handled = true;
        }

        private void img1_ManipulationStarted(object sender, ManipulationStartedRoutedEventArgs e)
        {
            e.Handled = true;
        }

        private void img1_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
        {
            if (forceManipulationsToEnd)
            {
                e.Complete();
                return;
            }
            _rotate.Angle += (e.Delta.Rotation * 180 / Math.PI);
            _scale.ScaleX = _scale.ScaleY *= e.Delta.Scale;
            _scale.CenterX = _rotate.CenterX = img1.ActualWidth / 2;
            _scale.CenterY = _rotate.CenterY = img1.ActualHeight / 2;
            _translate.X += e.Delta.Translation.X;
            _translate.Y += e.Delta.Translation.Y;
            e.Handled = true;
        }

        private void img1_ManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e)
        {
            e.Handled = true;
            btn1.Content = _translate.Y;
            TextToShow(_translate.X, _translate.Y, img1);
        }

        private void img2_ManipulationStarting(object sender, ManipulationStartingRoutedEventArgs e)
        {
            rectangle(img2);
            forceManipulationsToEnd = false;
            e.Handled = true;
        }

        private void img2_ManipulationStarted(object sender, ManipulationStartedRoutedEventArgs e)
        {
            e.Handled = true;
        }

        private void img2_ManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e)
        {
            e.Handled = true;
            btn1.Content = _translate.Y;
            TextToShow(_translate.X, _translate.Y, img2);
        }

        private void img2_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
        {
            if (forceManipulationsToEnd)
            {
                e.Complete();
                return;
            }
            _rotate.Angle += (e.Delta.Rotation * 180 / Math.PI);
            _scale.ScaleX = _scale.ScaleY *= e.Delta.Scale;
            _scale.CenterX = _rotate.CenterX = img2.ActualWidth / 2;
            _scale.CenterY = _rotate.CenterY = img2.ActualHeight / 2;
            _translate.X += e.Delta.Translation.X;
            _translate.Y += e.Delta.Translation.Y;
            e.Handled = true;
        }

        private void img3_ManipulationStarting(object sender, ManipulationStartingRoutedEventArgs e)
        {
            rectangle(img3);
            forceManipulationsToEnd = false;
            e.Handled = true;
        }

        private void img3_ManipulationStarted(object sender, ManipulationStartedRoutedEventArgs e)
        {
            e.Handled = true;
        }

        private void img3_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
        {
            if (forceManipulationsToEnd)
            {
                e.Complete();
                return;
            }
            _rotate.Angle += (e.Delta.Rotation * 180 / Math.PI);
            _scale.ScaleX = _scale.ScaleY *= e.Delta.Scale;
            _scale.CenterX = _rotate.CenterX = img3.ActualWidth / 2;
            _scale.CenterY = _rotate.CenterY = img3.ActualHeight / 2;
            _translate.X += e.Delta.Translation.X;
            _translate.Y += e.Delta.Translation.Y;
            e.Handled = true;
        }

        private void img3_ManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e)
        {
            e.Handled = true;
            btn1.Content = _translate.Y;
            TextToShow(_translate.X, _translate.Y, img3);
        }

        private void rectangle(Image r)
        {
            _translate = new TranslateTransform()
            {
                X = 0,
                Y = 0
            };

            _scale = new ScaleTransform()
            {
                ScaleX = 1,
                ScaleY = 1,
                CenterX = r.ActualWidth / 2,
                CenterY = r.ActualHeight / 2
            };

            _rotate = new RotateTransform()
            {
                Angle = 0,
                CenterX = r.ActualWidth / 2,
                CenterY = r.ActualHeight / 2
            };

            _transformGroup = new TransformGroup();
            ((IList<Transform>)_transformGroup.Children).Add(_rotate);
            ((IList<Transform>)_transformGroup.Children).Add(_scale);
            ((IList<Transform>)_transformGroup.Children).Add(_translate);

            r.RenderTransform = _transformGroup;
        }

        private void TextToShow(double x, double y, Image i)
        {

            if (i.Margin.Left == 167)
            {
                if ((x > 441) && (x < 558))
                {
                    ToolService(grid1, i);
                    CalculateYaxis(y);
                    _translate.X = 493;
                }
                else if ((x > 660) && (x < 793))
                {
                    ToolService(grid2, i);
                    CalculateYaxis(y);
                    _translate.X = 722;
                }
                else if ((x > 887) && (x < 1002))
                {
                    ToolService(grid3, i);
                    CalculateYaxis(y);
                    _translate.X = 944;
                }
            }
            else if (i.Margin.Left == 329)
            {
                if ((x > 270) && (x < 390))
                {

                    ToolService(grid1, i);
                    CalculateYaxis(y);
                    _translate.X = 331;
                }
                else if ((x > 513) && (x < 615))
                {
                    ToolService(grid2, i);
                    CalculateYaxis(y);
                    _translate.X = 557;
                } 
                else if ((x > 722) && (x < 840))
                {
                    ToolService(grid3, i);
                    CalculateYaxis(y);
                    _translate.X = 781;
                }
            }

        }

        private void ToolService(GridView g, Image i)
        {
            if ((ToolTipService.GetToolTip(i).ToString() != null) && (ToolTipService.GetToolTip(i).ToString() != ""))
            {
                ToolTipService.SetToolTip((g), ToolTipService.GetToolTip(i).ToString());
            }
        }
        
        private void CalculateYaxis(double y)
        {
            if ((y > 305) && (y < 456))
            {
                _translate.Y = 377;
            }
            else if ((y > 152) && (y < 290))
            {
                _translate.Y = 222;
            }
            else if ((y > 5) && (y < 132))
            {
                _translate.Y = 70;
            }
        }

        private void Page_Loaded_1(object sender, RoutedEventArgs e)
        {
            
            logic();
        }

        public void Shuffle<T>(T[] array)
        {
            int n = array.Length;
            while (n > 1)
            {
                int k = rng.Next(n);
                n--;
                T temp = array[n];
                array[n] = array[k];
                array[k] = temp;
            }
        }

        public void logic()
        {
            if (title1 == "Animals")
            {
                word = question.Dequeue();
                pic = Picture.Dequeue();
                audioPlayer.Source = new Uri("ms-appx:/Audio/ArrangeTheWord.mp3", UriKind.Absolute);
                audioPlayer.Play();
            }
            else if (title1 == "Sports")
            {
                word = question1.Dequeue();
                pic = Picture1.Dequeue();
                audioPlayer.Source = new Uri("ms-appx:/Audio/ArrangeTheWord.mp3", UriKind.Absolute);
                audioPlayer.Play();
            }           
            Shuffle(Alphabets);
            char[] char1 = word.ToCharArray();
            randLetter[0] = char1[0].ToString();
            randLetter[1] = char1[1].ToString();
            randLetter[2] = char1[2].ToString();
            randLetter[3] = Alphabets[0].ToString();
            randLetter[4] = Alphabets[1].ToString();
            randLetter[5] = Alphabets[2].ToString();
            txtResult.Text = "";
            Shuffle(randLetter);
            picture.Source = new BitmapImage(new Uri(picDirectory + pic + fileType, UriKind.Absolute));
            img1.Source = new BitmapImage(new Uri(directory + randLetter[0] + fileType, UriKind.Absolute));
            ToolTipService.SetToolTip((img1), randLetter[0].ToString());
            img2.Source = new BitmapImage(new Uri(directory + randLetter[1] + fileType, UriKind.Absolute));
            ToolTipService.SetToolTip((img2), randLetter[1].ToString());
            img3.Source = new BitmapImage(new Uri(directory + randLetter[2] + fileType, UriKind.Absolute));
            ToolTipService.SetToolTip((img3), randLetter[2].ToString());
            img4.Source = new BitmapImage(new Uri(directory + randLetter[3] + fileType, UriKind.Absolute));
            ToolTipService.SetToolTip((img4), randLetter[3].ToString());
            img5.Source = new BitmapImage(new Uri(directory + randLetter[4] + fileType, UriKind.Absolute));
            ToolTipService.SetToolTip((img5), randLetter[4].ToString());
            img6.Source = new BitmapImage(new Uri(directory + randLetter[5] + fileType, UriKind.Absolute));
            ToolTipService.SetToolTip((img6), randLetter[5].ToString());

        }

        private void btnCheck_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                txt.Text = "";
                txt.Text += ToolTipService.GetToolTip(grid1).ToString();
                txt.Text += ToolTipService.GetToolTip(grid2).ToString();
                txt.Text += ToolTipService.GetToolTip(grid3).ToString();

               // if (txt.Text == word)
               // {
                    txt.Text = "";
                    grid1.Background = new SolidColorBrush(Colors.White);
                    grid2.Background = new SolidColorBrush(Colors.White);
                    grid3.Background = new SolidColorBrush(Colors.White);
                    //txtResult.Text = "Congrats! That was Awesome!";
                    //score += 10;
                   // txtScore.Text = "Score: " + score.ToString();
                    //btnCheck.Content = "Awesome!";
                    rectangle(img1);
                    rectangle(img2);
                    rectangle(img3);
                    rectangle(img4);
                    rectangle(img5);
                    rectangle(img6);
                    logic();
                //}
                //else
                //{
                //    txtResult.Text = "Please Try again!";
                //    audioPlayer.Source = new Uri("ms-appx:/Audio/TryAgain.mp3", UriKind.Absolute);
                //    audioPlayer.Play();
                //    if (ToolTipService.GetToolTip(grid1).ToString() == word.Substring(0, 1))
                //    {
                //        grid1.Background = new SolidColorBrush(Colors.Green);
                //    }
                //    else if (ToolTipService.GetToolTip(grid1).ToString() != word.Substring(0, 1))
                //    {
                //        grid1.Background = new SolidColorBrush(Colors.DeepPink);
                //    }


                //    if (ToolTipService.GetToolTip(grid2).ToString() != word.Substring(1, 1))
                //    {
                //        grid2.Background = new SolidColorBrush(Colors.DeepPink);
                //    }
                //    else if (ToolTipService.GetToolTip(grid2).ToString() == word.Substring(1, 1))
                //    {
                //        grid2.Background = new SolidColorBrush(Colors.Green);
                //    }


                //    if (ToolTipService.GetToolTip(grid3).ToString() != word.Substring(2))
                //    {
                //        grid3.Background = new SolidColorBrush(Colors.DeepPink);
                //    }
                //    else if (ToolTipService.GetToolTip(grid3).ToString() == word.Substring(2))
                //    {
                //        grid3.Background = new SolidColorBrush(Colors.Green);
                //    }

                    //btnCheck.Content = "You Stink!";
                //}
            }
            catch (Exception ex)
            { }
        }

        private void img4_ManipulationStarting(object sender, ManipulationStartingRoutedEventArgs e)
        {
            rectangle(img4);
            forceManipulationsToEnd = false;
            e.Handled = true;
        }

        private void img4_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
        {
            if (forceManipulationsToEnd)
            {
                e.Complete();
                return;
            }
            _rotate.Angle += (e.Delta.Rotation * 180 / Math.PI);
            _scale.ScaleX = _scale.ScaleY *= e.Delta.Scale;
            _scale.CenterX = _rotate.CenterX = img3.ActualWidth / 2;
            _scale.CenterY = _rotate.CenterY = img3.ActualHeight / 2;
            _translate.X += e.Delta.Translation.X;
            _translate.Y += e.Delta.Translation.Y;
        }

        private void img4_ManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e)
        {
            e.Handled = true;
            btn1.Content = _translate.Y;
            TextToShow(_translate.X, _translate.Y, img4);
        }

        private void img5_ManipulationStarting(object sender, ManipulationStartingRoutedEventArgs e)
        {
            rectangle(img5);
            forceManipulationsToEnd = false;
            e.Handled = true;
        }

        private void img5_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
        {
            if (forceManipulationsToEnd)
            {
                e.Complete();
                return;
            }
            _rotate.Angle += (e.Delta.Rotation * 180 / Math.PI);
            _scale.ScaleX = _scale.ScaleY *= e.Delta.Scale;
            _scale.CenterX = _rotate.CenterX = img3.ActualWidth / 2;
            _scale.CenterY = _rotate.CenterY = img3.ActualHeight / 2;
            _translate.X += e.Delta.Translation.X;
            _translate.Y += e.Delta.Translation.Y;
        }

        private void img5_ManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e)
        {
            e.Handled = true;
            btn1.Content = _translate.Y;
            TextToShow(_translate.X, _translate.Y, img5);
        }

        private void img6_ManipulationStarting(object sender, ManipulationStartingRoutedEventArgs e)
        {
            rectangle(img6);
            forceManipulationsToEnd = false;
            e.Handled = true;
        }

        private void img6_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
        {
            if (forceManipulationsToEnd)
            {
                e.Complete();
                return;
            }
            _rotate.Angle += (e.Delta.Rotation * 180 / Math.PI);
            _scale.ScaleX = _scale.ScaleY *= e.Delta.Scale;
            _scale.CenterX = _rotate.CenterX = img3.ActualWidth / 2;
            _scale.CenterY = _rotate.CenterY = img3.ActualHeight / 2;
            _translate.X += e.Delta.Translation.X;
            _translate.Y += e.Delta.Translation.Y;
        }

        private void img6_ManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e)
        {
            e.Handled = true;
            btn1.Content = _translate.Y;
            TextToShow(_translate.X, _translate.Y, img6);
        }

        private void btnBackward_Click(object sender, RoutedEventArgs e)
        {
            this.Frame.Navigate(typeof(Read), title1);
        }

        private void btnForward_Click(object sender, RoutedEventArgs e)
        {
            this.Frame.Navigate(typeof(Write), title1);
        }

        private void MainBtn_Click(object sender, RoutedEventArgs e)
        {
            this.Frame.Navigate(typeof(BlankPage));


            XmlDocument tileXml = TileUpdateManager.
            GetTemplateContent
            (TileTemplateType.TileWideText03);

            XmlElement textElement = (XmlElement)tileXml.
            GetElementsByTagName("text")[0];
            textElement.AppendChild(
            tileXml.CreateTextNode
            ("Unlocked Level 2"));

            TileUpdateManager.CreateTileUpdaterForApplication()
            .Update(new TileNotification(tileXml));
        }
    }
}
